from django.core.cache import cache
from django.views.generic.simple import direct_to_template
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from machete.forms import AdminCreatePageForm
from machete.models import Page,Category, Slideshow, Photo,\
                        LinkSlideshowPage, LinkPhotoSlideshow
from django.contrib.auth.decorators import user_passes_test

MEMCACHE_PAGES = 'pages'

@user_passes_test(lambda u: u.is_superuser)
def admin_home(request):
    return direct_to_template(request, 'machete/admin/index.html')

# Not used #
@user_passes_test(lambda u: u.is_superuser)
def admin_createpage(request):
    if request.method == 'POST':
        form = AdminCreatePageForm()
        if form.is_valid():
            page = form.save(commit=False)
            page.save()
            return HttpResponseRedirect('machete/admin/createpage.html')
    else:
        form = AdminCreatePageForm()
    return direct_to_template(request, 'machete/admin/createpage.html',
        {'form': form})

# Not used #
@user_passes_test(lambda u: u.is_superuser)
def admin_listpage(request):
    pages = cache.get(MEMCACHE_PAGES)
    if (pages is None or pages.count()==0) :
        pages = Page.objects.all()
        cache.add(MEMCACHE_PAGES, pages)
    return direct_to_template(request, 'machete/admin/listpage.html',
                              {'page_list': pages,})

# ======= Admin ManyToMany ======= #
# ======= Delete ===== #
@user_passes_test(lambda u: u.is_superuser)
def admin_deletelink_slideshowpage(request, entity_id):
    entity = get_object_or_404(LinkSlideshowPage, pk=entity_id)
    entity.delete()
    return HttpResponseRedirect(request.GET.get('return_url','/admin/'))

@user_passes_test(lambda u: u.is_superuser)
def admin_deletelink_photoslideshow(request, entity_id):
    entity = get_object_or_404(LinkPhotoSlideshow, pk=entity_id)
    entity.delete()
    return HttpResponseRedirect(request.GET.get('return_url','/admin/'))

# ======= Update ===== #
@user_passes_test(lambda u: u.is_superuser)
def admin_updatelink_photoslideshow(request, photo_id):
    entity = get_object_or_404(Photo, pk=photo_id)
    
    if request.method == 'POST':
        slideshow_id = request.POST.get('selected_slideshow','') # '' is the default value
        slideshow = get_object_or_404(Slideshow, pk=slideshow_id)
        alreadyInList = Slideshow.photos(slideshow).__contains__(entity)
        if not alreadyInList : # If slideshow doesnt contains the photo
            link = LinkPhotoSlideshow(photo=entity, slideshow=slideshow) # then add it
            link.save()
            
    return HttpResponseRedirect(request.POST.get('return_url','/admin/'))


@user_passes_test(lambda u: u.is_superuser)
def admin_updatelink_photoslideshowBySlideshow(request, slideshow_id):
    entity = get_object_or_404(Slideshow, pk=slideshow_id)
    
    if request.method == 'POST':
        photo_id = request.POST.get('selected_photo','') # '' is the default value
        photo = get_object_or_404(Photo, pk=photo_id)
        alreadyInList = Photo.slideshows(photo).__contains__(entity)
        if not alreadyInList : # If slideshow doesnt contains the photo
            link = LinkPhotoSlideshow(slideshow=entity, photo=photo) # then add it
            link.save()
            
    return HttpResponseRedirect(request.POST.get('return_url','/admin/'))

@user_passes_test(lambda u: u.is_superuser)
def admin_updatelink_slideshowpage(request, slideshow_id):
    entity = get_object_or_404(Slideshow, pk=slideshow_id)
    
    if request.method == 'POST':
        page_id = request.POST.get('selected_page','') # '' is the default value
        page = get_object_or_404(Page, pk=page_id)
        alreadyInList = Page.slideshows(page).__contains__(entity)
        if not alreadyInList :
            link = LinkSlideshowPage(slideshow=entity, page=page)
            link.save()
            
    return HttpResponseRedirect(request.POST.get('return_url','/admin/'))

@user_passes_test(lambda u: u.is_superuser)
def admin_updatelink_slideshowpageByPage(request, page_id):
    entity = get_object_or_404(Page, pk=page_id)
    
    if request.method == 'POST':
        slideshow_id = request.POST.get('selected_slideshow','') # '' is the default value
        slideshow = get_object_or_404(Slideshow, pk=slideshow_id)
        alreadyInList = Slideshow.photos(slideshow).__contains__(entity)
        if not alreadyInList :
            link = LinkSlideshowPage(page=entity, slideshow=slideshow)
            link.save()
            
    return HttpResponseRedirect(request.POST.get('return_url','/admin/'))

# ======= Admin Delete ======= #
@user_passes_test(lambda u: u.is_superuser)
def admin_deletepage(request, entity_id):
    entity = get_object_or_404(Page, pk=entity_id)
    entity.delete()
    return HttpResponseRedirect('/admin/admin_listpage')

@user_passes_test(lambda u: u.is_superuser)
def admin_deletecategory(request, entity_id):
    entity = get_object_or_404(Category, pk=entity_id)
    entity.delete()
    return HttpResponseRedirect('/admin/admin_listcategory')

@user_passes_test(lambda u: u.is_superuser)
def admin_deleteslideshow(request, entity_id):
    entity = get_object_or_404(Slideshow, pk=entity_id)
    entity.delete()
    return HttpResponseRedirect('/admin/admin_listslideshow')

@user_passes_test(lambda u: u.is_superuser)
def admin_deletephoto(request, entity_id):
    entity = get_object_or_404(Photo, pk=entity_id)
    entity.delete()
    return HttpResponseRedirect('/admin/admin_listphoto')